@page "/Account/AccessDenied"

@using Microsoft.AspNetCore.Identity
@using Passwordless.AdminConsole.Identity
@using Passwordless.Common.Extensions

@inject IHttpContextAccessor HttpContextAccessor
@inject SignInManager<ConsoleAdmin> SignInManager

<Page Title="Access Denied">
    <p>Oh, this shouldn't happen.</p>
    <p>Sometimes it happens by mistake and you can <a class="link-blue" href="@ReturnUrl">try again</a>.</p>
</Page>

@code {
    private string? _returnUrl;
    
    [SupplyParameterFromQuery(Name = "ReturnUrl")]
    public string? ReturnUrl
    {
        get => _returnUrl;
        set
        {
            if (value != null && !value.IsLocalUrl())
            {
                _returnUrl = null;
            }
            else
            {
                _returnUrl = value;
            }
        }
    }

    protected override async Task OnInitializedAsync()
    {
        var claimsPrincipal = HttpContextAccessor.HttpContext!.User;
        var user = await SignInManager.UserManager.GetUserAsync(claimsPrincipal);
        await SignInManager.RefreshSignInAsync(user);
    }
}